package com.taxigang.client.ui.map;

import com.google.code.gwt.geolocation.client.Coordinates;
import com.google.code.gwt.geolocation.client.Geolocation;
import com.google.code.gwt.geolocation.client.Position;
import com.google.code.gwt.geolocation.client.PositionCallback;
import com.google.code.gwt.geolocation.client.PositionError;
import com.google.code.gwt.geolocation.client.PositionOptions;
import com.google.gwt.user.client.Window;
import com.taxigang.client.Session;
import com.taxigang.client.TaxigangMode;
import com.taxigang.client.ui.map.SearchView.SearchDisplayMode;
import com.taxigang.client.uibinder.TaxiGangMapPanel;
import com.taxigang.client.uibinder_mobile.TaxiGangMobile;
import com.taxigang.shared.RouteInfo;

public class Geolocator {
	private SearchView searchView;
	private TaxiGangMapPanel  taxiGangMapPanel;
	private TaxiGangMobile  taxiGangMobile;
	public void runGeolocation(SearchView searchView, TaxiGangMapPanel  taxiGangMapPanel) {
		this.searchView = searchView;
		this.taxiGangMapPanel = taxiGangMapPanel;
		taxiGangMobile = null;

		ProcessingDialog.hide();

		if (!Geolocation.isSupported()) {
			Window.alert("Obtaining Geolocation FAILED! Geolocation API is not supported.");
			return;
		}
		final Geolocation geo = Geolocation.getGeolocation();
		if (geo == null) {
			Window.alert("Obtaining Geolocation FAILED! Object is null.");
			return;
		}

		obtainPosition(geo);
	}

	public void runGeolocation(SearchView searchView, TaxiGangMobile  taxiGangMobile) {
		this.searchView = searchView;
		this.taxiGangMobile = taxiGangMobile;
		this.taxiGangMapPanel = null;

		ProcessingDialog.hide();

		if (!Geolocation.isSupported()) {
			Window.alert("Obtaining Geolocation FAILED! Geolocation API is not supported.");
			return;
		}
		final Geolocation geo = Geolocation.getGeolocation();
		if (geo == null) {
			Window.alert("Obtaining Geolocation FAILED! Object is null.");
			return;
		}

		obtainPosition(geo);
	}

	private void obtainPosition(Geolocation geo) {
		//Window.alert("Allow your browser to locate you or decline");
		geo.getCurrentPosition(new PositionCallback() {
			public void onFailure(PositionError error) {
				Window.alert("Couldn't locate you. You can enter the start location manually");
				String message = "";
				switch (error.getCode()) {
				case PositionError.UNKNOWN_ERROR:
					message = "Unknown Error";
					break;
				case PositionError.PERMISSION_DENIED:
					message = "Permission Denied";
					break;
				case PositionError.POSITION_UNAVAILABLE:
					message = "Position Unavailable";
					break;
				case PositionError.TIMEOUT:
					message = "Time-out";
					break;
				default:
					message = "Unknown error code.";
				}
				
				if (TaxigangMode.PARTNER == Session.getInstance().getMode()){
					//set level
					final RouteInfo partnerInfo = Session.getInstance().getRoute();
					String destination = partnerInfo.getDestination();
					if (taxiGangMapPanel != null){
						NavigatorManager.createStartMarker(destination,searchView, taxiGangMapPanel, true);
					}else{
						MobileNavigatorManager.createStartMarker(destination, taxiGangMobile);						
					}

				}

			}

			public void onSuccess(Position position) {
				final Coordinates c = position.getCoords();
				final String partnerRef = Session.getInstance().getRef();
				if (taxiGangMapPanel != null){
					NavigatorManager.createStartMarker(c, searchView, taxiGangMapPanel, true,null);
				}else{
					MobileNavigatorManager.createStartMarker(c, taxiGangMobile, true, null);						
				}
				if (partnerRef!=null){
					searchView.setMode(SearchDisplayMode.SEARCH_CREATE);

				} else {
					searchView.setMode(SearchDisplayMode.SEARCH_CREATE);

				}


			}
		}, PositionOptions.getPositionOptions(false, 15000, 30000));
	}

}
